<?php
namespace app\common\repositories\member;
use app\common\repositories\BaseRepository;
use app\common\dao\member\MemberSettingDao as dao;
use app\common\repositories\user\UserRepository;
use think\exception\ValidateException;

/**
 * @mixin dao
 */
class MemberSettingRepository extends BaseRepository
{
    public function __construct(dao $dao)
    {
        $this->dao = $dao;
    }


    /**
     * 检查用户的会员权限,返回对应的金额和会员等级
     * @return array
     */
    public function getCasePrice($uid,$type)
    {
        $memberType = $this->getUserMemberType($uid);
        $memberSetting = $this->dao->getWhere(['type'=>$memberType,'case_type'=>$type]);

        $old = $this->dao->getWhere(['type'=>0,'case_type'=>$type]);

        if(!$memberSetting){
            throw new ValidateException('会员套餐有误！请联系管理员');
        }

        return [
            'memberType'=>$memberType,
            'price'=>$memberSetting['price'],
            'old_price'=>$old['price']
        ];
    }


    public function getUserMemberType($uid)
    {
        $user = app()->make(UserRepository::class)->get($uid);

        if(!$user) return 0;

        if($user->member_time_2 > time()){
            return 2;
        }

        if($user->member_time > time()){
            return  1;
        }

        return 0;
    }


}